# sql_copy.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net
#
use strict; use warnings;

package CCN::sql_copy::Jifty;
use base qw(CCN::sql_copy);

sub initialize_sqlite3($$)
{
    my $sqlite3_file = shift;
    my $env          = shift;

    $env->{DBNAME} = $sqlite3_file;
    $env->{DRIVER} = 'SQLite';

    $env->{HOST} = 'localhost' if not exists $env->{HOST};
    $env->{USER} = '' if not exists $env->{USER};
    $env->{PASS} = '' if not exists $env->{PASS};

    __initialize_jifty($env);
}

sub initialize_mysql($$)
{
    my $mysql_dbname = shift;
    my $env          = shift;
    
    $env->{DBNAME} = $mysql_dbname;
    $env->{DRIVER} = 'MySQL';

    $env->{HOST} = 'localhost' if not exists $env->{HOST};
    $env->{USER} = '' if not exists $env->{USER};
    $env->{PASS} = '' if not exists $env->{PASS};

    __initialize_jifty($env);
}

sub copy
{
    my $db_from = shift;
    my $db_to   = shift;
    my $env     = shift;
    # a hahsref; if defined, provide values for the #copy statements (otherwise the #copy statements are not used)


    # Collect --#copy statements.
    my $where = '';

  YYY TBD-URGENT

        if( /^\s*--\s*#copy\s*(\S.*)$/i )
        {
            $where = $1;
            next;
        }

        $query_from .= qq( $where) if defined $env;
        __copy_table($table,$query_from) unless $where =~ /^DONT/;
  
    __set_version($db_to,$version);
}


sub __initialize_jifty($)
{
    my $env = shift;

    use File::Temp;
    my $tmp = new File::Temp( UNLINK => 1, SUFFIX => '.yml' );

    $env->{YML} = $tmp->filename;

    # Create a configuration file.
    print $tmp template_text(__yml(),$env);
    close $tmp;

    # Force Jifty to use our configuration file.
    $ENV{JIFTY_CONFIG} = $env->{YML};

    chdir '/home/manager/jifty/'.$env->{APPNAME}
        or die "CCN-ERROR: Package $env->{APPNAME} not properly installed.";

    use Jifty::Script::Schema;
    my $s = new Jifty::Script::Schema;
    $s->run();

    # Remove the temporary (.yml) file.
    undef $tmp;
}


# This is only a minimal Jifty configuration file to get "schema --setup" to work.
# The full config for the web application is generated in Application::jifty.

sub __yml
{
    return <<'YML';
--- 
framework: 
  AdminMode: 0
  DevelMode: 0
  ApplicationClass: _{APPCLASS}_
  ApplicationName: _{APPNAME}_
  ApplicationUUID: _{APPUUID}_
  Database: 
    CheckSchema: 1
    Database: _{DBNAME}_
    Driver: _{DRIVER}_
    Host: _{HOST}_
    User: '_{USER}_'
    Password: '_{PASS}_'
    RecordBaseClass: Jifty::DBI::Record::Cachable
    Version: 0.0.1
  L10N: 
    PoDir: share/po
  LogLevel: DEBUG
  Mailer: Sendmail
  MailerArgs: []

  Plugins: 
    - 
      SkeletonApp: {}
    - 
      REST: {}
    - 
      Halo: {}
    - 
      ErrorTemplates: {}
    - 
      CompressedCSSandJS: {}
    -
        Authentication::Password: {}
    -
        User: {}

YML
}

1;